package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.entity.Goods;
import com.mall.mapper.GoodsMapper;
import com.mall.service.IGoodsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ShouPeng
 * @since 2024-01-14
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
    private final String GOODS_IS_SALE = "goods_is_sale" ;
    private final String GOODS_CREATE_TIME = "goods_create_time" ;


    final
    GoodsMapper goodsMapper;

    public GoodsServiceImpl(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    /**
     * 查询商品
     * @param page 页码
     * @return goods
     */
    @Override
    public IPage<Goods> queryGoods(Page<Goods> page) {
        return goodsMapper.selectPage(page,null);
    }


    /**
     * 新品推荐
     * @return goods
     */
    @Override
    public List<Goods> queryGoodsNew() {
        return goodsMapper.selectList(new QueryWrapper<Goods>().eq(GOODS_IS_SALE,true).orderByDesc(GOODS_CREATE_TIME).last("limit 10"));
    }

    /**
     * @param categoryId
     * @return goods
     */
    @Override
    public IPage<Goods> queryGoodsByCategoryId(Page<Goods> page,String categoryId) {
        return goodsMapper.selectPage(page,new QueryWrapper<Goods>().eq("goods_category_id",categoryId));
    }

    /**
     * @param key
     * @return goods
     */
    @Override
    public IPage<Goods> searchGoods(Page<Goods> page,String key) {
        return goodsMapper.selectPage(page,new QueryWrapper<Goods>().like("goods_name", key).or().like("goods_content_markdown", key));
    }

    /**
     * 查询某一个商品的详细信息
     * @return A Goods
     */
    @Override
    public Goods queryGoodsInfo(String goodsId) {
        return goodsMapper.selectById(goodsId);
    }

    /**
     * 减少库存
     *
     * @param count
     * @param goodsId
     * @return
     */
    @Override
    public boolean minusGoodsStock(Integer count, String goodsId) {
        return goodsMapper.minusGoodsStock(count, goodsId);
    }


}
